import * as dto from './DTO';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';
const baseUrl = '/sales/api/v1';

const getInitdesc = formatMessage({
    id: 'api.message.getInitdesc',
    defaultMessage: '初始化'
});
const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询'
});
const getForSAPDesc = formatMessage({
    id: 'api.message.getForSAPDesc',
    defaultMessage: '查询要单'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询详情'
});
const exportDesc = formatMessage({
    id: 'api.exportDesc',
    defaultMessage: '导出'
});
//初始化
export const getInitData = () => request(`${baseUrl}/ui/saleDeliveryOrder/init`, null, dto.InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getInitdesc));

// 查询订单列表
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/saleDeliveryOrders?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 要单
export const getForSAP = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/saleDeliveryOrders/forSAP?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getForSAPDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/saleDeliveryOrders/${id}`, null, dto.Detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
// 导出
export const exports = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/saleDeliveryOrders/export?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc))
        .then(res => res.data);
};
